﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Auth.Utility;
using MediatR;
using Microsoft.Extensions.DependencyInjection;

namespace Auth.Core.DependencyInjection
{
    /// <summary>
    /// AspNet Core DI Config
    /// </summary>
    public static class DependencyInjectionConfig
    {
        /// <summary>
        /// AspNet Core DI Extension
        /// </summary>
        /// <param name="services"></param>
        public static void AddDIConfiguration(this IServiceCollection services)
        {
            // 批量注入
            List<Assembly> assemblies = AssemblyUtility.GetAssemblies(new List<string> { "Auth.Application", "Auth.Repository" });

            foreach (Assembly assembly in assemblies)
            {
                foreach(var item in assembly.ExportedTypes.Where(s => s.IsClass && !s.IsSealed))
                {
                    services.AddTransient(item.GetInterfaces().Where(s => s.Name.EndsWith(item.Name)).FirstOrDefault(), item);
                }
            }
        }
    }
}
